/*!
\file GraphVisualizer.cpp
\brief Contains implementation of GraphVisualizer class.
\ingroup GUI
*/

#include"GraphVisualizer.h"

GraphVisualizer::GraphVisualizer( DManager *graphManager, QWidget *parent ):QWidget( parent ){
	m_graphManager = graphManager;
	connect( m_graphManager->communicator(), SIGNAL( accessNode(int) ), parent, SLOT( _accessNode(int) ) );
	connect( m_graphManager->communicator(), SIGNAL( accessEdge(int) ), parent, SLOT( _accessEdge(int) ) );
	connect( m_graphManager->communicator(), SIGNAL( containerRemove(int)), parent, SLOT(_removeContainer(int) ) );
	connect( m_graphManager->communicator(), SIGNAL(newContainerElement(int, int)), parent, SLOT(_containerNewElement(int,int)) );
	connect( m_graphManager->communicator(), SIGNAL(removeContainerElement(int,int)), parent, SLOT(_containerRemoveElement(int,int)) );
	connect( m_graphManager->communicator(), SIGNAL(setContainerNode(int,int,int, QString)), parent, SLOT(_containerSetNode(int,int,int, QString)) );
	connect( m_graphManager->communicator(), SIGNAL(setContainerEdge(int,int,int,int,QString)), parent, SLOT(_containerSetEdge(int,int,int,int,QString)) );
	connect( m_graphManager->communicator(), SIGNAL(setContainerData(int,int,QString)), parent, SLOT(_containerSetData(int,int,QString)) );
	connect( m_graphManager->communicator(), SIGNAL(logComment(QString)), parent, SLOT(_logComment(QString)) );
	
	m_layout = new QVBoxLayout( this );
	setLayout( m_layout );
	
	m_label = new QLabel;
	m_label->setPixmap( m_graphManager->toQPixmap() );
	
	m_scrollArea = new QScrollArea(this);
	m_scrollArea->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
	m_scrollArea->setWidgetResizable(true);
	m_scrollArea->setWidget(m_label);
	
	m_layout->addWidget( m_scrollArea );
	
	m_buttonWidget = new QWidget(this);
	m_layout->addWidget(m_buttonWidget);
	
	m_buttonLayout = new QHBoxLayout(m_buttonWidget);
	m_buttonWidget->setLayout(m_buttonLayout);
	
	m_button = new QPushButton( "Step", m_buttonWidget );
	m_buttonLayout->addWidget( m_button );
	connect( m_button, SIGNAL(clicked()), this, SLOT(_requestUpdate()) );
	
	m_playButton = new QPushButton("Play", m_buttonWidget);
	m_buttonLayout->addWidget( m_playButton );
	connect( m_playButton, SIGNAL(clicked()), this, SLOT(_requestPlay()) );
	
	m_stopButton = new QPushButton("Stop", m_buttonWidget);
	m_buttonLayout->addWidget(m_stopButton);
	connect( m_stopButton, SIGNAL(clicked()), this, SLOT(_requestStop()) );
}

GraphVisualizer::~GraphVisualizer(){

}

void GraphVisualizer::_requestUpdate(){
	emit requestUpdate();
}

void GraphVisualizer::_requestPlay(){
	emit requestPlay();
}

void GraphVisualizer::_requestStop(){
	emit requestStop();
}

void GraphVisualizer::update( Action *a ){
	a->exec( m_graphManager->communicator() );
	if( !a->isUninteresting() )
		m_label->setPixmap( m_graphManager->toQPixmap() );
}

